package cc.htdf.gwc.datacompare.repository.impl;

import cc.htdf.gwc.datacompare.repository.DateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by JT on 2017/10/11.
 */
@Repository
public class DateRepositoryImpl implements DateRepository {

    private static final String TABLE_NAME_CONFIG_COMPARE_HOUR = "GWC_CONFIG_DATA_COMPARE_HOUR";
    private static final String[] TABLE_COLUMNS_CONFIG_COMPARE_HOUR = new String[] {"LAST_COMPARE_HOUR"};

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询上一次对比的实况数据发布时间
     * @return
     */
    @Override
    public String findLastCompareDate() {
        String columns = TABLE_COLUMNS_CONFIG_COMPARE_HOUR[0];
        String sql = String.format("SELECT %s FROM %s", columns, TABLE_NAME_CONFIG_COMPARE_HOUR);
        return jdbcTemplate.queryForObject(sql, String.class);
    }

    /**
     * 更新上一次对比的实况数据发布时间
     * @param date：实况数据发布时间
     */
    @Override
    public void updateLastCompareDate(String date) {
        String columns = TABLE_COLUMNS_CONFIG_COMPARE_HOUR[0];
        String sql = String.format("UPDATE %s SET %s = ?", TABLE_NAME_CONFIG_COMPARE_HOUR, columns);
        Object[] params = new Object[]{date};
        jdbcTemplate.update(sql, params);
    }
}
